﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml.Linq;
using System.Globalization;

namespace IridiumDB.Repository
{
    public class BaseXmlRepository
    {
        protected String ApplicationDataPath
        {
            get
            {
                return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "mobileflares");
            }
        }

        protected String GetDataFilePath(String filename)
        {
            return Path.Combine(ApplicationDataPath, filename);
        }

        protected String SetUpDataFile(String filename, String xmlRoot, String schemaVersion, bool enforceNewSchema)
        {
            String _datafile = GetDataFilePath(filename);
            if (!File.Exists(_datafile) || (enforceNewSchema && !IsEqualVersion(_datafile, xmlRoot, schemaVersion)))
            {
                Directory.CreateDirectory(Path.GetDirectoryName(_datafile));
                using (var stream = File.Create(_datafile))
                {
                    using (var writer = new StreamWriter(stream))
                    {
                        XDocument doc = new XDocument(
                                                new XDeclaration("1.0", "utf-8", "yes"),
                                                new XElement(xmlRoot, new XAttribute("version", schemaVersion))
                                            );
                        doc.Save(writer);
                    }
                }
            }
            return _datafile;
        }


        private bool IsEqualVersion(String path, String xmlRoot, String schemaVersion)
        {
            var doc = XDocument.Load(path);
            var versionAttribute = doc.Element(xmlRoot).Attribute("version");
            if (versionAttribute == null) return false;
            return schemaVersion.Equals(versionAttribute.Value);
        }

        protected int GetInt(XElement element, String elementName)
        {
            String temp = element.Element(elementName) == null ? null : element.Element(elementName).Value;
            return temp == null ? default(int) : int.Parse(temp, CultureInfo.InvariantCulture);
        }

        protected float GetFloat(XElement element, String elementName)
        {
            String temp = element.Element(elementName) == null ? null : element.Element(elementName).Value;
            return temp == null ? default(float) : float.Parse(temp, CultureInfo.InvariantCulture);
        }

        protected DateTime GetDateTime(XElement element, String elementName)
        {
            String temp = element.Element(elementName) == null ? null : element.Element(elementName).Value;
            return temp == null ? default(DateTime) : DateTime.Parse(temp, CultureInfo.InvariantCulture);
        }

        protected String GetString(XElement element, String elementName)
        {
            return element.Element(elementName) == null ? null : element.Element(elementName).Value;
        }

        protected Guid GetGuid(XElement element, String elementName)
        {
            String guidTemp = element.Element(elementName) == null ? null : element.Element(elementName).Value;
            return guidTemp == null ? Guid.Empty : new Guid(guidTemp);
        }
    }
}
